package net.wanglu.www.zzz.rule.sva;

import java.util.HashMap;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.nsubj._NSubjTuple;
import edu.stanford.nlp.trees.TreeGraphNode;

public class EndWithIngsAsSubj extends _SvaRule {

	public static final Pattern EndWithIngs = Pattern.compile("\\bbelongings\\b|\\bbookings\\b|\\bclippings\\b|\\bdiggings\\b|\\bdoings\\b|\\bearnings\\b|\\bfilings\\b|\\bfindings\\b|\\blodgings\\b|\\bsavings\\b|\\bshavings\\b|\\bsightings\\b|\\bsurroundings\\b|\\bsweepings\\b|\\btakings\\b|\\btidings\\b|\\bwinnings\\b", Pattern.CASE_INSENSITIVE);

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( super.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		_NSubjTuple nst = (_NSubjTuple) objects[1];
		if ( !"NP".equals(nst.getSubjectAncestor().value())) return Rule.Inapplicable;
		return Rule.Applicable;
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		_NSubjTuple nst =  (_NSubjTuple) objects[1];
		
		if ( this.applicable(preprocessData, nst) == Rule.Inapplicable) return Rule.Inapplicable;
		TreeGraphNode subj = (TreeGraphNode) nst.getSubj();
		TreeGraphNode verb = (TreeGraphNode) nst.getVerb();
		TreeGraphNode cop = (TreeGraphNode) nst.getCop();
		TreeGraphNode closestAux = (TreeGraphNode) nst.getAux();
		
		TreeGraphNode pred = verb;
		if ( cop != null) pred = cop;
		if ( closestAux != null) pred = closestAux;
		String predWord = pred.label().word();
		String predTag = pred.label().tag();

		if ( EndWithIngsAsSubj.EndWithIngs.matcher(subj.label().word()).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		}
		return Rule.Inapplicable;
	}
	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("某些以ings结尾的名词，这类名词作主语时，谓语动词通常用复数形式。这类名词有：belongings，bookings，clippings，diggings，doings，earnings，filings，findings，lodgings，savings，shavings，sightings，surroundings，sweepings，takings，tidings，winnings等。");
		sb.append("<br>");
		sb.append("注意：在一定的语言环境里，少数以ings结尾的名词也可以根据概念一致原则作单数用。例如：tidings、innings。");
		sb.append("</p>");
		return sb.toString();
	}
}
